module(..., package.seeall) 

-- seed the random number generator.  
-- Probably overkill, but we only do it once (:-) 

function set_randomseed () 
   local d = os.date() .. os.clock () 
   local t = {string.byte(d,1,string.len(d))}
   local c = 1 
   for i = 1, #t do 
      _,c = math.modf(math.exp((math.log(t[i]*c))/2))
   end 
   math.randomseed (math.modf(c*10000000000000))
end

set_randomseed ()

-- fill a table of length len
-- with integers randomly chosen  
-- between lower bound lb and upper bound ub (inclusive).  

function random_table (lb, ub, len) 
   local t = {} 
   for i = 1,len do 
      t[i] = math.random(lb,ub)
   end
   return t
end 

function random_boolean(prob) 
   local x = math.random() 
   return (x<= prob)
end

function random_subset(ub, approx_card) 
   local prob = approx_card/ub
   local result = {} 

   for i = 1,ub do 
      if random_boolean(prob)
      then 
	 table.insert(result,i)
      end
   end
   return result 
end